﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Canyala.Lagoon.Extensions
{
    public static class FrameworkElementExtenstions
    {
        public static void AnimateOpacity(this FrameworkElement element, double from, double to, double speed=1, IEasingFunction easing=null, Storyboard storyboard=null )
            { AnimateDouble(element, from, to, speed, easing, storyboard, "(UIElement.Opacity)"); }

        public static void AnimateWidth(this FrameworkElement element, double from, double to, double speed=1, IEasingFunction easing=null, Storyboard storyboard=null )
            { AnimateDouble(element, from, to, speed, easing, storyboard, "(FrameworkElement.Width)"); }

        public static void AnimateHeight(this FrameworkElement element, double from, double to, double speed=1, IEasingFunction easing=null, Storyboard storyboard=null )
            { AnimateDouble(element, from, to, speed, easing, storyboard, "(FrameworkElement.Height)"); }

        public static void AnimateTranslateX(this FrameworkElement element, double from, double to, double speed=1, IEasingFunction easing=null, Storyboard storyboard=null )
            { AnimateDouble(element, from, to, speed, easing, storyboard, "(UIElement.RenderTransform).(CompositeTransform.TranslateX)"); }

        public static void AnimateTranslateY(this FrameworkElement element, double from, double to, double speed=1, IEasingFunction easing=null, Storyboard storyboard=null )
            { AnimateDouble(element, from, to, speed, easing, storyboard, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)"); }
        
        public static void AnimateScaleX(this FrameworkElement element, double from, double to, double speed=1, IEasingFunction easing=null, Storyboard storyboard=null )
            { AnimateDouble(element, from, to, speed, easing, storyboard, "(UIElement.RenderTransform).(CompositeTransform.ScaleX)"); }

        public static void AnimateScaleY(this FrameworkElement element, double from, double to, double speed=1, IEasingFunction easing=null, Storyboard storyboard=null )
            { AnimateDouble(element, from, to, speed, easing, storyboard, "(UIElement.RenderTransform).(CompositeTransform.ScaleY)"); }

        private static void AnimateDouble(this FrameworkElement element, double from, double to, double speed, IEasingFunction easing, Storyboard storyboard, string propertyPath)
        {
            var animation = new DoubleAnimation { From = from, To = to, SpeedRatio = speed, EasingFunction = easing };
            Storyboard.SetTarget(animation, element);
            Storyboard.SetTargetProperty(animation, new PropertyPath(propertyPath));
            
            if (storyboard == null)
            {
                storyboard = new Storyboard();
                storyboard.Children.Add(animation);
                storyboard.Begin();
            }
            else
                storyboard.Children.Add(animation);
        }
    }
}
